﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class EvecScheduleEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                string id = Request.QueryString["id"];
                if (!string.IsNullOrEmpty(id))
                {
                    ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                    ReqEvectionSchedule reqEvectionSchedule = ctx.ReqEvectionSchedules.Single(r => r.Id == int.Parse(id));
                    PreStartTime.Value = reqEvectionSchedule.PreStartTime.ToString("yyyy-MM-dd");
                    PreEndTime.Value = reqEvectionSchedule.PreEndTime.ToString("yyyy-MM-dd");
                    LeavePoint.Value = reqEvectionSchedule.LeavePoint;
                    ArrivePoint.Value = reqEvectionSchedule.ArrivePoint;
                    TrafficTool.Value = reqEvectionSchedule.TrafficTool;
                    Cause.Value = reqEvectionSchedule.Cause;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            ReqEvectionSchedule reqEvectionSchedule;
            string id = Request.QueryString["id"];
            if(string.IsNullOrEmpty(id))
            {
                reqEvectionSchedule = new ReqEvectionSchedule();
                reqEvectionSchedule.FormNo = Request.QueryString["formNo"];
                ctx.ReqEvectionSchedules.InsertOnSubmit(reqEvectionSchedule);
            }
            else
            {
                reqEvectionSchedule = ctx.ReqEvectionSchedules.Single(r => r.Id ==int.Parse(id));
            }
            reqEvectionSchedule.PreStartTime = DateTime.Parse(PreStartTime.Value);
            reqEvectionSchedule.PreEndTime =DateTime.Parse(PreEndTime.Value);
            reqEvectionSchedule.LeavePoint = LeavePoint.Value;
            reqEvectionSchedule.ArrivePoint = ArrivePoint.Value;
            reqEvectionSchedule.TrafficTool = TrafficTool.Value;
            reqEvectionSchedule.Cause = Cause.Value;
            ctx.SubmitChanges();
            Response.Write("<script>window.returnValue='Succeed';window.close();</script>");
        }
    }
}